package com.test.io.bio.task;

import com.test.io.utils.ByteArrayUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * describe: 监听任务
 *
 * @author lianying
 * @date 2020/01/21
 */
public class ListenerTask implements Runnable{
    private Socket socket;
    public ListenerTask(Socket socket){
        this.socket=socket;
    }
    @Override
    public void run() {
        try {
            InputStream inputStream = socket.getInputStream();
            //头
            byte [] head=new byte[4];
            inputStream.read(head);
            int length= ByteArrayUtils.byteArray2Int(head);
            byte [] content=new byte[length];
            inputStream.read(content);
            inputStream.close();
            String contentString=new String(content,"UTF-8");
            System.out.println("服务接收到消息："+contentString.toLowerCase());
        } catch (IOException e) {
            System.out.println("未知错误");
        }
    }
}
